home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Best of MacTutor - S…e Code for Volumes 1 to 5
/
The Best of MacTutor - Source Code for Volume 1-5 (Wayzata Technology)(6031)(1990).bin
/
Source Code
/
#07 (Mar 86)
/
Pascal 2-3
/
Small Flight Source
/
Small Flight.pas
< prev
next >
Wrap
Pascal/Delphi Source File
|
1986-02-13
|
5KB
|
115 lines
program smallflight; { a scaled-down "star flight" }
{ Written by Mike Morton for MacTutor }
{ Converted to TML Pascal by David E. Smith}
{$I MemTypes.ipas} { TML Mac Iibraries }
{$I QuickDraw.ipas} { Quickdraw interface }
{$I OSIntf.ipas } { Operating System interface }
{$I ToolIntf.ipas } { Toolbox interface }
const
numStars = 140; { number of stars we display }
maxXY = 64; { largest star radius (X or Y) }
maxZ = 200; { largest star distance (Z) }
speed = 4; { Z change per animation cycle }
type
star = record { information about one star }
ploc: point; { physical location in space (X, Y) }
z: integer; { physical location in space (Z) }
sloc: point; { location on the screen (h, v) }
end;
var { global program variables }
stars: array [0..numStars] of star; { information on stars }
bounds: rect; { rectangle used for bounds checking }
sorigin: point; { center of screen }
myPort: grafPort; { our graphics environment }
anevent: eventrecord; { for checking if the user's bored }
procedure flipPix (h, v: integer); external; { 68000 routine to flip pixel }
{ makestar -- randomize physical location; set Z; find screen location. }
procedure makeStar (VAR new: star); { initialize one star }
var dh, dv: integer; { star's position, relative to origin }
begin;
new.ploc.h := random mod maxXY; { horizontal position }
new.ploc.v := random mod maxXY; { vertical position }
new.z := maxZ; { how far away is it? }
dh := new.ploc.h*maxZ div new.z; { compute h offset }
new.sloc.h := sorigin.h + dh; { and compute absolute h position }
dv := new.ploc.v*maxZ div new.z; { compute v offset }
new.sloc.v := sorigin.v + dv; { and compute absolute v position }
flipPix (new.sloc.h, new.sloc.v); { flip that spot (draw star 1st time) }
end; { of procedure makeStar }
{ initialize -- Do Mac initializations; calculate display rect; initialize
screen; define bounds rect; draw initial stars. }
procedure initialize; { one-time initialization }
var i: integer; { star number }
begin;
initGraf(@thePort); { fire up quickdraw }
openPort(@myPort); { get a drawing environment }
initCursor; { get rid of the Finder's "watch" }
bounds := screenbits.bounds; { start with the whole screen }
insetRect (bounds, 25, 30); { shrink it in a bit }
sorigin.h := (bounds.left + bounds.right) div 2; { find the... }
sorigin.v := (bounds.top + bounds.bottom) div 2; { ...origin }
eraseRect (myPort.portRect); { clean the screen }
invertRect (bounds); { space is black }
offsetrect (bounds, -sorigin.h, -sorigin.v); { center bounds on origin }
for i := 1 to numStars do { loop through all the stars.. }
makeStar (stars [i]); { ...and make up each one }
end; { procedure init }
{ cycle -- main routine. For each star, erase the old position. Then see
if its motion has carried it past the plane we're in. If so, we create a
new star. If not, we compute the new apparent position from the new Z.
If the apparent position is outside the display, we create a new star;
otherwise we draw the star's new position. }
procedure cycle; { do one animation cycle }
var
i: integer; { star number in main loop }
dv, dh: integer; { star coordinates, origin-relative }
sp: ^star; { fast pointer to stars[i] }
begin;
for i := 1 to numStars do { loop through all the stars }
begin;
sp := @stars[i]; { point to star (avoid subscripting) }
flipPix (sp^.sloc.h, sp^.sloc.v); { erase the star's old position }
sp^.z := sp^.z - speed; { time advances: find new z position }
if sp^.z <= 0 { past the plane of the eye yet? }
then makeStar (sp^) { yes: this star's gone; make another }
else begin; { no: update star's screen position }
dh := sp^.ploc.h*maxZ div sp^.z; { compute relative h }
sp^.sloc.h := sorigin.h + dh; { and compute absolute screen h }
dv := sp^.ploc.v*maxZ div sp^.z; { compute relative v }
sp^.sloc.v := sorigin.v + dv; { and compute absolute screen v }
if (dv >= bounds.bottom) { is the new position... }
or (dv <= bounds.top) { ...outside... }
or (dh >= bounds.right) { ...the bounds rectangle... }
or (dh <= bounds.left) { ...which is centered at the origin? }
then makeStar (sp^) { yes: out of sight, so get a new one }
else flipPix (sp^.sloc.h, sp^.sloc.v) { no: draw it at new position }
end; { of case where z didn't go off edge }
end; { of loop through all stars }
end; { of procedure cycle }
begin; { main program }
initialize; { set everything up }
flushEvents (everyevent, 0); { ignore stale events }
repeat { main loop: }
cycle; { do one animation "frame" }
until getnextevent (mDownMask+keyDownMask,anevent); { until click or key }
end.